package org.example.demo3.controller;

import org.example.demo3.common.*;
import org.example.demo3.info.*;
import org.example.demo3.service.Articleservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/article")
public class Articlecontroller {
    @Resource
    private Hot_count hotCount;
    @Resource
    private Nice_User niceUser;
    @Resource
    private Response_Tree responseTree;
    @Resource
    private Session_delete sessionDelete;
    @Resource
    private Articleservice articleservice;
//主页博客列表
    @RequestMapping("/getarticle")
    public Ajaxtext getarticle(HttpServletRequest request,String type){
        Userinfo userinfo= Session.getsession(request);
        List<Articleinfo> list;
        if (type.equals("all")) {
            list = articleservice.getarticle(userinfo);
        }else {
            list=articleservice.getarticle_by_type(userinfo.getId(),type);
        }
        if (list==null){
            return Ajaxtext.success("无博客内容",list);
        }
        return Ajaxtext.success("博客内容",list);
    }
//    删除博客
    @RequestMapping("/delete")
    public Ajaxtext delete(HttpServletRequest request,Integer id){
        Integer result=articleservice.delete(id);
        if (result>0){
            return Ajaxtext.success("删除成功",result);
        }
        return Ajaxtext.fail("删除失败",-1);
    }
//    获取博客内容
    @RequestMapping("/getarticlecontent")
    public Ajaxtext getarticlecontent(Integer id){
        Articleinfo_plus articleinfo_plus=articleservice.getarticlecontent(id);
        if (articleinfo_plus!=null){
            return Ajaxtext.success("查询成功",articleinfo_plus);
        }
        return Ajaxtext.fail("查询失败",-1);
    }
//    浏览次数
    @RequestMapping("/rcount")
    public Ajaxtext rcount(Integer id){
        int result=articleservice.rcount(id);
        if (result>0){
            return Ajaxtext.success("修改成功",result);
        }
        return Ajaxtext.fail("修改失败",0);
    }
//    修改博客
    @RequestMapping("/edit_article")
    public Ajaxtext edit_article(Articleinfo articleinfo){
        LocalDateTime now=LocalDateTime.now();
        articleinfo.setUpdatetime(now);
        int result=articleservice.edit_article(articleinfo);
        if (result>=0){
            return Ajaxtext.success("修改或添加成功",result);
        }else {
            return Ajaxtext.fail("操作失败",-1);
        }
    }
//    增加博客
    @RequestMapping("/add_article")
    public Ajaxtext add_article(Articleinfo articleinfo,HttpServletRequest request){
        articleinfo.setUid(Session.getsession(request).getId());
        int result=articleservice.add_article(articleinfo,Session.getsession(request).getId());
        if (result>=0){
            return Ajaxtext.success("修改或添加成功",result);
        }else {
            return Ajaxtext.fail("操作失败",-1);
        }
    }
//    获取博客大厅页数
    @RequestMapping("/allpage")
    public Ajaxtext allpage(HttpServletRequest request){
        int a=articleservice.allpage();
        int result;
        if ((a%4)!=0){
            result=(a/4)+1;
        }else {
            result=(a/4);
        }
        if (result>=0){
            System.out.println(Session.getsession(request));
            if (Session.getsession(request)==null){
                return Ajaxtext.success(600,"查询成功", result);
            }
            return Ajaxtext.success(800,"查询成功", result);
        }else {
            return Ajaxtext.fail("查询失败",-1);
        }
    }
//    博客大厅列表
    @RequestMapping("/this_page_content")
    public Ajaxtext this_page_content(int index,int page){
        List<Articleinfo_plus> list=articleservice.this_page_content(index,((page-1)*index));
        if (list!=null){
            return Ajaxtext.success("查询成功",list);
        }else {
            return Ajaxtext.fail("查询失败",-1);
        }
    }
    @RequestMapping("/setresponse")
    public Ajaxtext setresponse(HttpServletRequest request, Responseinfo responseinfo){
        Userinfo userinfo=Session.getsession(request);
        responseinfo.setResponsename(userinfo.getUsername());
        Integer result=articleservice.setresponse(responseinfo);
        if (result>0){
            return Ajaxtext.success("回复成功",result);
        }else {
            return Ajaxtext.fail("回复失败",-1);
        }
    }
    @RequestMapping("/getresponse")
    public Ajaxtext getresponse(Integer reviewid){
        List<Responseinfo> list=articleservice.getresponse(reviewid);
        if (list!=null){
            return Ajaxtext.success("查询成功",list);
        }
        else {
            return Ajaxtext.fail("查询失败",-1);
        }
    }
    @RequestMapping("/setreview")
    public Ajaxtext setreview(HttpServletRequest request, Reviewinfo reviewinfo){
        Userinfo userinfo=Session.getsession(request);
        reviewinfo.setReviewname(userinfo.getUsername());
        Integer result=articleservice.setreview(reviewinfo);
        if (result>0){
            return Ajaxtext.success("评论成功",1);
        }
        return Ajaxtext.fail("评论失败",-1);
    }
    @RequestMapping("/getreview")
    public Ajaxtext getreview(Integer id){
        List<Reviewinfo> list=articleservice.getreview(id);
        if (list!=null){
            return Ajaxtext.success("查询成功",list);
        }
        else {
            return Ajaxtext.fail("查询失败",-1);
        }
    }
    @RequestMapping("/deletereview")
    public Ajaxtext deleteview(HttpServletRequest request,Integer id){
        if (!sessionDelete.verify_review(request,id)){
            return Ajaxtext.fail("没有权限",-2);
        }
        int result=articleservice.deletereview(id);
        articleservice.deletereview_response(id);
        if (result>0){
            return Ajaxtext.success("删除成功",result);
        }else {
            return Ajaxtext.fail("删除失败",-1);
        }
    }
    @RequestMapping("/deleteresponse")
    public Ajaxtext deleteresponse(HttpServletRequest request,Integer id){
        if (!sessionDelete.verify_response(request,id)){
            return Ajaxtext.fail("没有权限",-2);
        }
        int result=articleservice.deleteresponse(id);
        if (result>0){
            return Ajaxtext.success("删除成功",result);
        }else {
            return Ajaxtext.fail("删除失败",-1);
        }
    }
    @RequestMapping("/get_response_res")
    public Ajaxtext get_response_res(Responseinfo responseinfo) {
        HashMap<Object,Object> hashMap=responseTree.response_tree_cons(responseinfo.getResponseid());
        System.out.println(hashMap);
        return Ajaxtext.success("获取回复成功", hashMap);
    }
    @RequestMapping("/setresponse_res")
    public Ajaxtext setresponse_res(HttpServletRequest request,Responseinfo responseinfo){
        Userinfo userinfo=Session.getsession(request);
        responseinfo.setResponsename(userinfo.getUsername());
        int result=articleservice.setresponse_res(responseinfo);
        if (result>0){
            return Ajaxtext.success("回复成功",result);
        }else {
            return Ajaxtext.fail("回复失败",result);
        }
    }
    @RequestMapping("/type_content")
    public Ajaxtext type_content(HttpServletRequest request){
        Userinfo userinfo=Session.getsession(request);
        List<String> list=articleservice.type_content(userinfo.getId());
        return Ajaxtext.success(list);
    }
    @RequestMapping("/nice_user")
    public Ajaxtext nice_user(){
        return Ajaxtext.success(niceUser.final_nice_user());
    }
    @RequestMapping("/hot_count")
    public Ajaxtext hot_count(){
        return Ajaxtext.success(hotCount.final_hot_article());
    }

}
